package com.tpc.client.systeme.core.datasource;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.dao.core.City;
import com.tpc.client.systeme.core.service.AddressService;
import com.tpc.client.systeme.core.service.AddressServiceAsync;

public class CityDataSource extends GwtRpcDataSource {

    String strurl = null;

    public CityDataSource(String strID) {
            setID(strID);

            DataSourceTextField cityNameField = new DataSourceTextField("cityName", " ");
            DataSourceIntegerField cityIdField = new DataSourceIntegerField("cityId");
            cityIdField.setPrimaryKey(Boolean.TRUE);
            cityIdField.setHidden(Boolean.TRUE);
            DataSourceTextField stateNameField = new DataSourceTextField("stateName", " ");
            DataSourceIntegerField stateIdField = new DataSourceIntegerField("stateId");
            stateIdField.setHidden(Boolean.TRUE);
            DataSourceTextField countryNameField = new DataSourceTextField("countryName", " ");
            DataSourceIntegerField countryIdField = new DataSourceIntegerField("countryId");
            countryIdField.setHidden(Boolean.TRUE);

            setFields(cityNameField,cityIdField,stateNameField,stateIdField,countryNameField,countryIdField);

            strurl = Page.getAppDir();
            strurl = strurl.substring(0,strurl.length()-1);
            strurl = strurl.substring(strurl.lastIndexOf("/"));
    }

     @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
       Criteria criteria = request.getCriteria();
       String strCity = null;
       String cityId = null;
       if (criteria != null) {
           if (criteria.getAttributeAsString("cityTerm") != null) {
               strCity = criteria.getAttributeAsString("cityTerm");
           }
           if (criteria.getAttributeAsString("cityIdTerm") != null) {
               cityId = criteria.getAttributeAsString("cityIdTerm");
           }
       }
        AddressServiceAsync service = GWT.create(AddressService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.address/addloader");
        service.loadListCity(strCity,cityId,new AsyncCallback<City[]> () {
            public void onFailure (Throwable caught) {
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);
            }
            public void onSuccess (City[] result) {
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    for (int i = 0; i < result.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("cityName", result[i].getName());
                        list[i].setAttribute("cityId", result[i].getId());
                        list[i].setAttribute("stateName", result[i].getStateName());
                        list[i].setAttribute("stateId", result[i].getStateId());
                        list[i].setAttribute("countryName", result[i].getCountryName());
                        list[i].setAttribute("countryId", result[i].getCountryId());
                    }
                }
                response.setData(list);
                processResponse(requestId, response);
            }
        });
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}
